var addTimer = function(){
    var list = [],
        interval;
    var nowTime = $('.nowTime').val();
    return function(id,timeStamp){

        if(!interval){
            interval = setInterval(go,1000);
        }

        list.push({ele:document.getElementById(id),time:timeStamp});
    }

    function go() {
        //  console.log(list);
        for (var i = 0; i < list.length; i++) {
            var a = changeTimeStamp(list[i].time);//返回剩余时间或者已截止
            var types= $.isArray(a);
            if(types){
                $('.day').html(a[0]);
                $('.hour').html(a[1]);
                $('.minute').html(a[2]);
                $('.second').html(a[3]);

            }else{
                clearInterval(interval);
                $('.gopre').css('display','block');
                $('.Countdown').css('display','none');
                console.log("已截止");
                var  url = $('.indexUrl').val();
                console.log(url);
              //  window.location.href = url;
                console.log("2");

            }
            if (!list[i].time)
                list.splice(i--, 1);
        }
    }

    //传入unix时间戳，得到倒计时
    function changeTimeStamp(timeStamp){
        console.log(timeStamp);

        var distancetime = new Date(timeStamp*1000).getTime() - new Date(nowTime*1000).getTime();
        if(distancetime > 0){
            //如果大于0.说明尚未到达截止时间
            var day = Math.floor(distancetime/(24*3600*1000));
            var hour = Math.floor((distancetime%(24*3600*1000))/(3600*1000));
            var min = Math.floor((distancetime%(3600*1000))/(60*1000));
            var sec =Math.floor((distancetime%(60*1000))/(1000));

            if(day<10){
                day = "0"+day;
            }
            if(sec<10){
                sec = "0"+ sec;
            }
            if(min<10){
                min = "0"+ min;
            }
            if(hour<10){
                hour = "0"+ hour;
            }

            var arr = new Array();
            arr[0] = day;
            arr[1] = hour;
            arr[2] = min;
            arr[3] = sec;
            nowTime++;
            return arr;
        }else{
            //若否，就是已经到截止时间了
            return "已截止！"
        }
    }
}();

var coursetime = $(".coursetime").val();//截止时间时间戳
addTimer("day",coursetime);

